//定义电影放映厅相关接口
const express = require("express");
const router = express.Router();
//引入mysql连接池
const pool = require("../utils/db");
//引入joi表单验证模块
const Joi = require("joi");
//引入响应对象模块
const Response = require("../utils/Response");
//执行将排片计划设置为未发布状态
//http://localhost:3000/plan/no-publish
router.post("/plan/no-publish", (req, resp) => {
  let { id } = req.body;
  //表单验证
  let schema = Joi.object({
    id: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  let sql = "update showingon_plan set status=0 where id = ?";
  pool.query(sql, [id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok());
  });
});
//执行将排片计划设置为发布状态
//http://localhost:3000/plan/publish
router.post("/plan/publish", (req, resp) => {
  let { id } = req.body;
  //表单验证
  let schema = Joi.object({
    id: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  let sql = "update showingon_plan set status=1 where id = ?";
  pool.query(sql, [id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok());
  });
});
//删除计划接口
//http:localhost:3000/plan/del
router.post("/plan/del", (req, resp) => {
  let { id } = req.body;
  //表单验证
  let schema = Joi.object({
    id: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  //执行删除业务
  let sql = "delete from showingon_plan where id = ?";
  pool.query(sql, [id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok());
  });
});
//根据id查找放映厅
//接口地址/cinema-room/sel
router.get("/plans/roomid", (req, resp) => {
  let { room_id } = req.query;
  //表单验证
  let schema = Joi.object({
    room_id: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  let sql = `select 
	sp.id plan_id,
	sp.cinema_id cinema_id,
	sp.cinema_room_id cinema_room_id,
	sp.movie_id movie_id,
	sp.showingon_date	showingon_date,
	sp.showingon_time showingon_time,
	sp.price price,
	sp.status status,
	mi.title title
from showingon_plan sp JOIN movie_info mi on sp.movie_id =mi.id
where 
cinema_room_id =? and showingon_date>?
order by  
showingon_date,showingon_time`;
  let now = new Date();
  let year = now.getFullYear();
  let month =
    now.getMonth() + 1 < 10 ? "0" + (now.getMonth() + 1) : now.getMonth();
  let day = now.getDate() ? "0" + now.getDate() : now.getDate();
  let time = `${year}-${month}-${day}`;
  pool.query(sql, [room_id, time], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  });
});
//添加放映厅接口
//接口地址/cinema/add
router.post("/plan/add", (req, resp) => {
  let {
    cinema_id,
    cinema_room_id,
    movie_id,
    showingon_date,
    showingon_time,
    status,
    price,
  } = req.body;
  //表单验证
  let schema = Joi.object({
    cinema_room_id: Joi.string().required(),
    cinema_id: Joi.string().required(),
    movie_id: Joi.string().required(),
    showingon_date: Joi.string()
      .required()
      .pattern(new RegExp("\\d{4}-\\d{2}-\\d{2}")),
    showingon_time: Joi.string()
      .required()
      .pattern(new RegExp("\\d{2}:\\d{2}")),
    status: Joi.string().required(),
    price: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return;
  }
  //表单验证通过，执行添加操作
  let sql = `insert into showingon_plan (
    cinema_id,
    cinema_room_id,
    movie_id,
    showingon_date,
    showingon_time,
    status,
    price) values (?,?,?,?,?,?,?)`;
  pool.query(
    sql,
    [
      cinema_id,
      cinema_room_id,
      movie_id,
      showingon_date,
      showingon_time,
      status,
      price,
    ],
    (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      resp.send(Response.ok());
    }
  );
});

//将Router接口导出
module.exports = router;
